package com.CGFrame.struts.bean.project;

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.regex.Pattern;

import com.CGFrame.struts.POJOs.Elementgroup;
import com.CGFrame.struts.POJOs.Project;
import com.CGFrame.struts.POJOs.Projectconfig;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.POJOs.Shotgroup;
import com.CGFrame.struts.POJOs.Status;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class ProjectInition {
	
	private ProPathConst ppc = new ProPathConst();

	private String fs = System.getProperties().getProperty("file.separator");

	public String mkProjectDir(ProjectFileBean prbean) {
		String path = null;

		String fpath = prbean.getFilePath();
		StringBuffer bf = new StringBuffer();
		bf.append(fpath);
		if (fpath.indexOf(fs) == -1) {
			bf.append(fs);
		}
		// Project
		bf.append(prbean.getPrjectName()).append(fs).append(
				ppc.getParam1());
		// scenes
		bf.append(fs).append(ppc.getParam2()).append(fs);

		try {
			mkdictory(bf.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		path = bf.toString();
		return path;

	}

	public void mkAssetsDir(ProjectFileBean prbean, String proPath) {
		
		  String rootPath = prbean.getRootPath();
	        rootPath += fs+"project"+fs+"Element";
	        FoldCopy fc = new FoldCopy();

		StringBuffer bf = new StringBuffer();
		// scenes
		bf.append(proPath).append(fs);
		// Assets
		bf.append(ppc.getParam6());
		mkdictory(bf.toString());
		// Production
		try {
			System.out.println("mkAssetsDir 开始将"+rootPath+ "拷贝到"+bf+" ："+System.currentTimeMillis());
			fc.copyDirectiory(rootPath, bf.toString());
			System.out.println("mkAssetsDir 完成将"+rootPath+ "拷贝到"+bf+" ："+System.currentTimeMillis());
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		bf.append(fs).append(ppc.getParam4()).append(fs);
		
		// Characters
		mkdictory(bf.toString() + ppc.getParam7());
		// Props
		mkdictory(bf.toString() + ppc.getParam8());
		// SceneSet
		mkdictory(bf.toString() + ppc.getParam9());

	}

	public void mkEpsDir(ProjectFileBean prbean, String proPath) {
		
		String espResult = prbean.getEspResult();
		String scenceResult = prbean.getScenceResult();
		String shotResult = prbean.getShotResult();
		String[] espArray = null;
		String[] scencetmp = null;
		String[] shottmp = null;
		if (prbean.isEspCheck()) {
			espArray = espResult.split(";");
		}
		if (prbean.isScenceCheck()) {
			scencetmp = scenceResult.split(";");
		}
		shottmp = shotResult.split(";");

		String path = proPath + ppc.getParam3() + fs;
        String rootPath = prbean.getRootPath();
        rootPath += fs+"project"+fs+"Shot";
        FoldCopy fc = new FoldCopy();
		if (prbean.isEspCheck()) {// 有集
			System.out.println("有集");
			
			for (int i = 0; i < espArray.length; i++) {

				String espPath = path
						+ getName(prbean.getEspName(), espArray[i]) + fs;
						
				try {
					System.out.println("mkEpsDir 开始将"+rootPath+ "拷贝到"+espPath+" ："+System.currentTimeMillis());
					fc.copyDirectiory(rootPath, espPath);
					System.out.println("mkEpsDir 完成将"+rootPath+ "拷贝到"+espPath+" ："+System.currentTimeMillis());
				} catch (IOException e) {
					
					e.printStackTrace();
				}
				
				espPath +=ppc.getParam4()+ fs;
				int shotnum = 1;

				String[] shotArray = null;
				if (prbean.isScenceCheck()) {
					shotArray = shottmp[i].split(",");
				} else {
					shotArray = shottmp;
				}
				if (prbean.isScenceCheck()) { // 有场
					System.out.println("有场");
				
					String scencePath = null;
					String[] scenceArray = scencetmp[i].split(",");

					if (prbean.isSingleShot()) { // 场独立
						System.out.println("场独立");
						for (int k = 0; k < scenceArray.length; k++) {
							scencePath = espPath
									+ getName(prbean.getScenceName(),
											scenceArray[k]);
							for (int j = 0; j < new Integer(shotArray[k]); j++) {
								if (prbean.isContShot()) { // 有集、有场、场独立、镜头连续
									System.out.println("镜头连续");
									String str =scencePath
									    + fs
								     	+ prbean.getProjectShortName()
									    + _getName(prbean.getEspName(),	espArray[i])
									    + _getName(prbean.getScenceName(),scenceArray[k])
									    + _getName(prbean.getShotName(),String.valueOf(shotnum))+fs+ppc.getParam10()+fs;
									
									
									mkdictory(str+ppc.getParam11());
									mkdictory(str+ppc.getParam12());
									shotnum++;
								} else { // 有集、有场、场独立、镜头不连续
									System.out.println("镜头不连续");
									String  str =scencePath
									      + fs
									      + prbean.getProjectShortName()
									      + _getName(prbean.getEspName(),espArray[i])
								          + _getName(prbean.getScenceName(),scenceArray[k])
									      + _getName(prbean.getShotName(),String.valueOf(j + 1))+fs+ppc.getParam10()+fs;
									
									mkdictory(str+ppc.getParam11());
									mkdictory(str+ppc.getParam12());
								} // if~else~
							}
						}
					} else { // 场不独立
						System.out.println("场不独立");
						scencePath = espPath + "Shot" + fs;
						for (int k = 0; k < scenceArray.length; k++) {
							for (int j = 0; j < new Integer(shotArray[k]); j++) {
								if (prbean.isContShot()) { // 有集、有场、场不独立、镜头连续
									System.out.println("镜头连续");
									String str =scencePath
									   + fs
									   + prbean.getProjectShortName()
								       + _getName(prbean.getEspName(),
											espArray[i])
									   + _getName(prbean.getScenceName(),
											scenceArray[k])
									   + _getName(prbean.getShotName(),
											String.valueOf(shotnum))+fs+ppc.getParam10()+fs;
									
									mkdictory(str+ppc.getParam11());
									mkdictory(str+ppc.getParam12());
									shotnum++;
								} else { // 有集、有场、场不独立、镜头不连续
									System.out.println("镜头不连续");
									String str =scencePath
									+ fs
									+ prbean.getProjectShortName()
									+ _getName(prbean.getEspName(),
											espArray[i])
									+ _getName(prbean.getScenceName(),
											scenceArray[k])
									+ _getName(prbean.getShotName(),
											String.valueOf(j + 1))+fs+ppc.getParam10()+fs;
									
									mkdictory(str+ppc.getParam11());
									mkdictory(str+ppc.getParam12());
								} // if~else~
							}
						}

					}// if ~ else ~

				} else { // 没场
					System.out.println("没场");
					for (int j = 0; j < new Integer(shotArray[i]); j++) {
						if (prbean.isContShot() || true) { // 有集、没场、 镜头连续  无论连不连续 都连续
							System.out.println("镜头连续");
							String shotPath = espPath + fs + ppc.getParam5() + fs;
							String str =shotPath
							+ prbean.getProjectShortName()
							+ _getName(prbean.getEspName(), espArray[i])
							+ _getName(prbean.getShotName(), String
									.valueOf(shotnum))+fs+ppc.getParam10()+fs;
							
							mkdictory(str+ppc.getParam11());
							mkdictory(str+ppc.getParam12());
							shotnum++;
						}
					}
				} // if~else~
			}// 集 ~for
		} else {// 没集
		
			String espPath = path + fs + ppc.getParam4() + fs;
			
			try {
				fc.copyDirectiory(rootPath, path);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
			int shotnum = 1;
			if (prbean.isScenceCheck()) { // 有场
				String scencePath = null;
				String[] scenceArray = scencetmp[0].split(",");
				String[] shotArray = shottmp[0].split(",");

				if (prbean.isSingleShot()) { // 场独立
					for (int k = 0; k < scenceArray.length; k++) {
						scencePath = espPath
								+ getName(prbean.getScenceName(),
										scenceArray[k]);
						for (int j = 0; j < new Integer(shotArray[k]); j++) {
							if (prbean.isContShot()) { // 没集、有场、场独立、镜头连续
								String str =scencePath
								+ fs
								+ prbean.getProjectShortName()
								+ _getName(prbean.getScenceName(),
										scenceArray[k])
								+ _getName(prbean.getShotName(), String
										.valueOf(shotnum))+fs+ppc.getParam10()+fs;
								
								mkdictory(str+ppc.getParam11());
								mkdictory(str+ppc.getParam12());
								shotnum++;
							} else { // 没集、有场、场独立、镜头不连续
								String str =scencePath
								+ fs
								+ prbean.getProjectShortName()
								+ _getName(prbean.getScenceName(),
										scenceArray[k])
								+ _getName(prbean.getShotName(), String
										.valueOf(j + 1))+fs+ppc.getParam10()+fs;
								
								mkdictory(str+ppc.getParam11());
								mkdictory(str+ppc.getParam12());
							} // if~else~
						}
					}
				} else { // 场不独立
					scencePath = espPath + ppc.getParam5() + fs;
					for (int k = 0; k < scenceArray.length; k++) {
						for (int j = 0; j < new Integer(shotArray[k]); j++) {
							if (prbean.isContShot()) { // 没集、有场、场不独立、镜头连续
								String str =scencePath
								+ fs
								+ prbean.getProjectShortName()
								+ _getName(prbean.getScenceName(),
										scenceArray[k])
								+ _getName(prbean.getShotName(), String
										.valueOf(shotnum))+fs+ppc.getParam10()+fs;
								
								mkdictory(str+ppc.getParam11());
								mkdictory(str+ppc.getParam12());
								shotnum++;
							} else { // 没集、有场、场不独立、镜头不连续
								String str =scencePath
								+ fs
								+ prbean.getProjectShortName()
								+ _getName(prbean.getScenceName(),
										scenceArray[k])
								+ _getName(prbean.getShotName(), String
										.valueOf(j + 1))+fs+ppc.getParam10()+fs;
								
								mkdictory(str+ppc.getParam11());
								mkdictory(str+ppc.getParam12());
							} // if~else~
						}
					}

				}// if ~ else ~

			} else { // 没集、没场、 镜头连续
				int num = 1;
				String shotPath = espPath + fs + ppc.getParam5() + fs;
				for (int i = 0; i < shottmp.length; i++) {
					for (int j = 0; j < new Integer(shottmp[i]); j++) {
						String str =shotPath
						+ prbean.getProjectShortName()
						+ _getName(prbean.getShotName(), String
								.valueOf(num))+fs+ppc.getParam10()+fs;
						
								mkdictory(str+ppc.getParam11());
								mkdictory(str+ppc.getParam12());
						num++;
					} // ~for~
				}
			} // if~else~
		}// if~ else~~~

	}

	public Project saveProject(ProjectFileBean prbean) {
		ProjectOperate poper = ProjectOperateFactory.getprojectOperate();
		Status projectStatus = (Status) poper.UniqueQuery("Status", "statusId,"
				+ "8");
		Project project = prbean.getProject();
		project.setStatus(projectStatus);
		try {
			poper.saveObject(project);
		} catch (Exception e) {
			project = null;
			e.printStackTrace();
		}
		return project;
	}

	public void saveAssets(ProjectFileBean prbean, Project project) {
		ProjectOperate poper = ProjectOperateFactory.getprojectOperate();
		Elementgroup eg = new Elementgroup();
		Status elementStatus = (Status) poper.UniqueQuery("Status", "statusId,"
				+ "21");
		eg.setElementGroupName(ppc.getParam6());
		eg.setProject(project);
		eg.setStatus(elementStatus);
		poper.saveObject(eg);

		Elementgroup characters = new Elementgroup();
		characters.setElementGroupName(ppc.getParam7());
		characters.setElementgroup(eg);
		characters.setProject(project);
		characters.setStatus(elementStatus);
		poper.saveObject(characters);

		Elementgroup props = new Elementgroup();
		props.setElementGroupName(ppc.getParam8());
		props.setElementgroup(eg);
		props.setProject(project);
		props.setStatus(elementStatus);
		poper.saveObject(props);

		Elementgroup senceSet = new Elementgroup();
		senceSet.setElementGroupName(ppc.getParam9());
		senceSet.setElementgroup(eg);
		senceSet.setProject(project);
		senceSet.setStatus(elementStatus);
		poper.saveObject(senceSet);

	}

	public void saveEpisodes(ProjectFileBean prbean, Project project) {
		String esp = prbean.getEspName();
		String espstr = prbean.getEspResult();
		String sce = prbean.getScenceName();
		String scestr = prbean.getScenceResult();
		String shot = prbean.getShotName();
		String shotstr = prbean.getShotResult();
		String[] espArr = null;
		if (prbean.isEspCheck()) {
			espArr = espstr.split(";");
		}
		String[] sceArr = null;
		if (prbean.isScenceCheck()) {
			sceArr = scestr.split(";");
		}
		String[] shotArr = shotstr.split(";");

		ProjectOperate poper = ProjectOperateFactory.getprojectOperate();
		Status shotStatus = (Status) poper.UniqueQuery("Status", "statusId,"
				+ "11");
		Status groupStatus = (Status) poper.UniqueQuery("Status", "statusId,"
				+ "21");

		Shotgroup sg = new Shotgroup();
		sg.setShotGroupName(ppc.getParam3());
		sg.setProject(project);
		sg.setStatus(groupStatus);
		poper.saveObject(sg);
		
		

		if (prbean.isEspCheck()) {// 有集
			for (int i = 0; i < espArr.length; i++) {
				int shotnum = 0;//是否连续
				Shotgroup shp = new Shotgroup();
				String espName = getName(esp, espArr[i]);
				shp.setShotGroupName(espName);
				shp.setProject(project);
				shp.setShotgroup(sg);
				shp.setStatus(groupStatus);
				poper.saveObject(shp);
				if (prbean.isScenceCheck()) {// 有集，有场
					String[] espSceShotArr = shotArr[i].split(",");
					String[] espSceArr = sceArr[i].split(",");
					for (int j = 0; j < espSceArr.length; j++) {
						String sceName = getName(sce, espSceArr[j]);
						Shotgroup sceshp = new Shotgroup();
						sceshp.setShotGroupName(sceName);
						sceshp.setProject(project);
						sceshp.setShotgroup(shp);
						sceshp.setStatus(groupStatus);
						poper.saveObject(sceshp);

						for (int k = 0; k < new Integer(espSceShotArr[j]); k++) {
							
							if(prbean.isContShot()){
								shotnum++;
							}else{
								shotnum =k+1;
							}
							String shotName = prbean.getProjectShortName()
									+ "_" + espName + "_" + sceName + "_"
									+ getName(shot, String.valueOf(shotnum));
							Shot shotob = new Shot();
							shotob.setShotName(shotName);
							shotob.setShotgroup(sceshp);
							shotob.setStatus(shotStatus);
							poper.saveObject(shotob);
						}
					}
				} else {
					
					for (int k = 0; k < new Integer(shotArr[i]); k++) {
						
						if(prbean.isContShot()){
							shotnum++;
						}else{
							shotnum =k+1;
						}
						
						String shotName = prbean.getProjectShortName() + "_"
								+ espName + "_"
								+ getName(shot, String.valueOf(shotnum));
						Shot shotob = new Shot();
						shotob.setShotName(shotName);
						shotob.setShotgroup(shp);
						shotob.setStatus(shotStatus);
						poper.saveObject(shotob);
					}

				}
			}
		} else {// 没集
			int shotnum = 0;//是否连续
			if (prbean.isScenceCheck()) {// 没集，有场
				String[] sceNums = sceArr[0].split(",");
				for (int j = 0; j < sceNums.length; j++) {
					String sceName = getName(sce, sceNums[j]);
					Shotgroup sceshp = new Shotgroup();
					sceshp.setShotGroupName(sceName);
					sceshp.setProject(project);
					sceshp.setShotgroup(sg);
					sceshp.setStatus(groupStatus);
					poper.saveObject(sceshp);
					String[] shotNames = shotArr[0].split(",");
					for (int k = 0; k < new Integer(shotNames[j]); k++) {
						if(prbean.isContShot()){
							shotnum++;
						}else{
							shotnum =k+1;
						}
						
						String shotName = prbean.getProjectShortName() + "_"
								+ sceName + "_"
								+ getName(shot, String.valueOf(shotnum));
						Shot shotob = new Shot();
						shotob.setShotName(shotName);
						shotob.setShotgroup(sceshp);
						shotob.setStatus(shotStatus);
						poper.saveObject(shotob);
					}

				}
			} else {
				for (int i = 0; i < shotArr.length; i++) {
					for (int k = 0; k < new Integer(shotArr[i]); k++) {
						shotnum++;
						String shotName = prbean.getProjectShortName() + "_"
								+ getName(shot, String.valueOf(shotnum));
						Shot shotob = new Shot();
						shotob.setShotName(shotName);
						shotob.setShotgroup(sg);
						shotob.setStatus(shotStatus);
						poper.saveObject(shotob);
						
					}
				}
			}

		}
	}

	// _esp000, 1 变成 esp001
	private String getName(String esp, String numstr) {
		String str1 = nameFormat(esp);
		String str2 = numFormat(new Integer(numstr), getNameNum(esp));
		return str1 + str2;
	}

	// _esp000, 1 变成 _esp001
	private String _getName(String esp, String numstr) {
		String str1 = nameFormat(esp);
		String str2 = numFormat(new Integer(numstr), getNameNum(esp));
		if(esp.indexOf("_") == 0){
			return "_" + str1 + str2;
		}else{
			return   str1 + str2;
		}
		
	}

	private File mkdictory(String fileName) {
		File file = new File(fileName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	private String numFormat(int value, int num) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumIntegerDigits(num);
		return nf.format(value).replaceAll(",", "");
	}

	private int getNameNum(String name) {
		if(name== null || name.equals("")){
			return 0;
		}
		Pattern pattern = Pattern.compile("[0-9]*");
		boolean b = true;
		int i = name.length() - 1;
		int num = 0;
		while (b) {
			char ch;
			if(i != -1){
			 ch = name.charAt(i);
			}else{
				return num;
			}
			b = pattern.matcher(String.valueOf(ch)).matches();
			i--;
			num++;
			if(!b){
				break;
			}
				
		}
		
		return --num;

	}

	// 把 _name000 变成 name
	private String nameFormat(String name) {
		if(name == null || name.equals("")){
			return "";
		}
		String tmp = null;
		if (name.indexOf('_') == 0) {
			tmp = name.substring(1, name.length());
		} else {
			tmp = name;
		}
		int num = getNameNum(tmp);
		return tmp.substring(0, name.length() - num - 1);
	}

	public static void main(String[] args) throws Exception {
		ProjectFileBean prbean = new ProjectFileBean();
		prbean.setProjectShortName("Test10");
		prbean.setFilePath("d:\\");
		prbean.setEspCheck(true);
		prbean.setScenceCheck(true);
		prbean.setEspName("_esp000");
		prbean.setScenceName("_sce000");
		prbean.setShotName("_shot00");
		prbean.setAllName("Test_esp000_sce000_shot00");
		prbean.setSingleShot(true);
		prbean.setContShot(true);
		prbean.setEspResult("1;2;");
		prbean.setScenceResult("2;3;");
		prbean.setShotResult("2;3;");
		// pl.mkEpsDir(prbean);
	}

	public void saveProjectConf(ProjectFileBean prbean, Integer projectId) {
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Projectconfig pconf = new Projectconfig();
		pconf.setProjectId(projectId);
		pconf.setProjectName(prbean.getPrjectName());
		if (prbean.isEspCheck()) {
			pconf.setHasEspCheck((short) 1);
		} else {
			pconf.setHasEspCheck((short) 0);
		}
		if (prbean.isScenceCheck()) {
			pconf.setHasScenceCheck((short) 1);
		} else {
			pconf.setHasScenceCheck((short) 0);
		}
		if (prbean.isSingleShot()) {
			pconf.setIsSingleShot((short) 1);
		} else {
			pconf.setIsSingleShot((short) 0);
		}
		if(prbean.isContShot()){
			pconf.setIsContShot((short)1);
		}else{
			pconf.setIsContShot((short)0);
		}

		pconf.setEspName(nameFormat(prbean.getEspName()));
		pconf.setEspNameNum((short)getNameNum(prbean.getEspName()));
		pconf.setScenceName(nameFormat(prbean.getScenceName()));
		pconf.setScenceNum((short)getNameNum(prbean.getScenceName()));
		pconf.setShotName(nameFormat(prbean.getShotName()));
		pconf.setShotNum((short)getNameNum(prbean.getShotName()));
		if(prbean.getShotName().indexOf("_") == 0){
			pconf.setIsShotUnderLine((short)1);
		}else{
			pconf.setIsShotUnderLine((short)0);
		}
		pop.saveObject(pconf);

	}

}
